package method.searchSQL;

//StockControlSys
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class criteriaControl {
	ArrayList<Criteria> criteria = new ArrayList<Criteria>();
	Criteria c ;
	
	public criteriaControl(){
	}
	
	
	/////***** Status Management *****/////
	
	public void addCurrentCriteria(String x , String y, String z, String w){
		c = new Criteria(x, y+"(Current)", "z", "w");
		criteria.add(c);
	}
	public void addCriteria(String databaseURL){
		try{
			HttpClient client = new DefaultHttpClient();
			HttpGet request = new HttpGet(databaseURL);
			HttpResponse response = client.execute(request);
			String line = "";
			String reply = "";
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					response.getEntity().getContent()));
			while ((line = reader.readLine()) != null) {
				reply += line;
			}
			reader.close();
			try {

				JSONArray json = new JSONArray(reply);
				JSONArray jsonArrayB = new JSONArray(json.getString(0));
				//crID 	characteristic 	description 	photo
				for(int i = 0; i< jsonArrayB.length(); i++){
					
					String getField = jsonArrayB.getString(i);
					JSONObject jsonField = new JSONObject(getField);
					String crIDValue  = jsonField.getString("crID");
					String characteristicValue = jsonField.getString("characteristic");
					String descriptionValue = jsonField.getString("description");
					String photoValue = jsonField.getString("photo");

					c = new Criteria(crIDValue, characteristicValue, descriptionValue, photoValue);
					criteria.add(c);					
				}


			} catch (JSONException e) {
				c = new Criteria("x", "y", "z", "w");
				criteria.add(c);
			}
		}
		catch (Exception e) {
			c = new Criteria("x", "y", "z", "w");
			criteria.add(c);
		}
		
	}

	public String getCriteria(int position){
		Criteria currentCriteria = criteria.get(position);
		String c = new String(""+currentCriteria.getcharacteristic());
		return c;
	}
	
	public int getSize(){
		return criteria.size();
	}
	
	public String searchCriteriaIDBycriteria(String criteriaValue){
		int index =-1;
		String y;
		Criteria x;
		String id;

		for(int i = 0; i < criteria.size() ; i++) {
			x = criteria.get(i);
			y = ""+x.getcharacteristic();
			if(y.equals(criteriaValue))
				index = i;
		}
		
		Criteria currentCriteria = criteria.get(index);
		String s = new String(""+currentCriteria.getcrID());
		
		return s;
	}
	
	public String toString(){
		String string = new String(""+criteria);
		return string;
	}
	
	public List getCriteriaList(){
		return criteria;
	}
	
///////////////////////no use////////////////////////////
	/*
	public int getIndex(Status s){
			return status.indexOf(s);
		}
	public Status getSearch(String g){
			return status.get(status.indexOf(g));
		}



	public String searchStatusObjectByStatus(String statusValue){
		int index =-1;
		String y;
		Status x;
		String id;

		for(int i = 0; i < status.size() ; i++) {
			x = status.get(i);
			y = ""+x.getstatus();
			if(y.equals(statusValue))
				index = i;
		}
		
		id = new String(""+status.get(index));
		return id;
	}
	 */
}
